//
//  RadioStationCollectionViewCell.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/19.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "RadioStationCollectionViewCell.h"
#import <UIImageView+WebCache.h>

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define WIDTH_PRO(X) (X)*(SCREEN_WIDTH/375)

#define HEIGHT_PRO(Y) (Y)*(SCREEN_HEIGHT/667)

@implementation RadioStationCollectionViewCell
- (void)dealloc
{
    [_tagImageView release];
    [_listenCountLabel release];
    [_image release];
    [_nameLabel release];
    [_numLabel release];
    [_playImageView release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews
{
    self.compere = YES;
    self.image = [UIImageView new];
    [self.contentView addSubview:self.image];
    [_image release];
    
    self.nameLabel = [UILabel new];
    self.nameLabel.textColor = [UIColor whiteColor];
    self.nameLabel.font = [UIFont systemFontOfSize:17 weight:1];
    [self.image addSubview:self.nameLabel];
    [_nameLabel release];
    
    self.numLabel = [UILabel new];
    self.numLabel.textColor = [UIColor whiteColor];
    self.numLabel.font = [UIFont systemFontOfSize:12 weight:0.5];
    [self.image addSubview:self.numLabel];
    [_numLabel release];
    
    self.playImageView = [UIImageView new];
    [self.image addSubview:self.playImageView];
    [_playImageView release];
    
    self.tagImageView = [UIImageView new];
    [self.image addSubview:self.tagImageView];
    [_tagImageView release];
    
    self.listenCountLabel = [UILabel new];
    self.listenCountLabel.textColor = [UIColor whiteColor];
    self.listenCountLabel.shadowColor = [UIColor blackColor];
    self.listenCountLabel.font = [UIFont systemFontOfSize:12];
    self.listenCountLabel.textAlignment = NSTextAlignmentRight;
    [self.image addSubview:self.listenCountLabel];
    [_listenCountLabel release];
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.image.frame = CGRectMake(0, 0, (SCREEN_WIDTH - WIDTH_PRO(30)) / 2, SCREEN_WIDTH / 4);
    
    self.nameLabel.frame = CGRectMake(self.image.frame.origin.x + self.image.frame.size.width / 2 - WIDTH_PRO(10), self.image.frame.size.height / 2 - HEIGHT_PRO(10), WIDTH_PRO(80), HEIGHT_PRO(20));
    self.numLabel.frame = CGRectMake(self.nameLabel.frame.origin.x, self.nameLabel.frame.origin.y + self.nameLabel.frame.size.height, WIDTH_PRO(100), HEIGHT_PRO(20));
    self.playImageView.frame = CGRectMake(self.image.frame.size.width + WIDTH_PRO(10) - WIDTH_PRO(50), self.image.frame.size.height - HEIGHT_PRO(40), WIDTH_PRO(40), HEIGHT_PRO(40));
    
    CGRect listenerRect = [self.listenCountLabel.text boundingRectWithSize:CGSizeMake(320, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:self.listenCountLabel.font,NSFontAttributeName, nil] context:nil];
    self.listenCountLabel.frame = CGRectMake(self.image.frame.size.width - listenerRect.size.width - WIDTH_PRO(10), HEIGHT_PRO(5), listenerRect.size.width, HEIGHT_PRO(20));
    
    self.tagImageView.frame = CGRectMake(self.image.frame.size.width - self.listenCountLabel.frame.size.width - WIDTH_PRO(25), HEIGHT_PRO(9), WIDTH_PRO(12), HEIGHT_PRO(12));
}


-(void)setRadioStationModel:(RadioStationModel *)radioStationModel
{
    if (_radioStationModel != radioStationModel) {
        [_radioStationModel release];
        _radioStationModel = [radioStationModel retain];
    }
    NSMutableArray *imageArray = [NSMutableArray array];
    for (int i = 1; i < 5; i++) {
        NSString *name = [NSString stringWithFormat:@"img_playing_%d", i];
        NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"png"];
        UIImage *image = [UIImage imageWithContentsOfFile:path];
        [imageArray addObject:image];
    }
    self.playImageView.animationImages = imageArray;
    self.playImageView.animationDuration = 0.5;
    self.playImageView.animationRepeatCount = 0;

    
    [self.image sd_setImageWithURL:[NSURL URLWithString:radioStationModel.pic_url_240_200] placeholderImage:[UIImage imageNamed:@"Read_zhanwei"]];
    self.nameLabel.text = radioStationModel.tag_name;
    self.numLabel.text = [NSString stringWithFormat:@"%@首", radioStationModel.quantity];
    self.playImageView.image = [UIImage imageNamed:@"songlist_play"];
    
    
    
    self.tagImageView.image = [UIImage imageNamed:@"iconfont-headphone"];
    self.listenCountLabel.text = [NSString stringWithFormat:@"%@", radioStationModel.listen_count];
    
    if ([radioStationModel.listen_count floatValue] > 10000) {
        CGFloat count = [radioStationModel.listen_count floatValue] / 10000;
        self.listenCountLabel.text = [NSString stringWithFormat:@"%.1f万", count];
    }
    
    if (radioStationModel.isCompare == YES
        ) {
        
        NSLog(@"11111");
        [self.playImageView startAnimating];
        radioStationModel.isCompare = NO;


        
    }else{
              NSLog(@"22222");
        [self.playImageView stopAnimating];
        self.playImageView.image = [UIImage imageNamed:@"songlist_play"] ;
        radioStationModel.isCompare = YES;
    }

}

@end
